***Analysis for Eyeglasses Impact BMJ Paper
***Xiaochen Ma
clear
set more off
capture log close

global data "/Users/apple/Dropbox (REAP)/REAP_Data/Data_upload/BMJ-MXC"

cd "$data"
use "Eyeglasses_China.dta",replace
*******************Table 1 and Table 2****************
******************************************************
local BALANCE age male baseusage aware0 harmvision0 gansu RE2 va18 zmath ///
pedu12 hhwealth bboarding pamigrant blackboard
bysort major: tab1 `BALANCE'

*** The Group Mean Part
	file open table1 using table1.txt, ///
		write text replace
	file write table1 "varname" _tab  "Edu mean" _tab  "No Edu mean" _tab  "Con mean" _tab  "Vou mean" _tab "Fre mean" _tab  _n
		
	foreach X of local BALANCE {

	
	// Edu stats
	
	summ `X' if edu == 1
	
	local mean_edu   	: display %-8.3f `r(mean)'
	local N_edu	 	    : display %-8.0f `r(N)'
	local V_edu 	 	: display %-8.3f `r(Var)'
	local SD_edu 	 	: display %-8.3f `r(sd)'

	// No Edu stats

	summ `X' if edu == 0
	
	local mean_noedu	: display %-8.3f `r(mean)'
	local N_noedu 		: display %-8.0f `r(N)'
	local V_noedu 		: display %-8.3f `r(Var)'
	local SD_noedu	 	: display %-8.3f `r(sd)'
	
    // Control stats
	
	summ `X' if con == 1
	
	local mean_con   	: display %-8.3f `r(mean)'
	local N_con	 	    : display %-8.0f `r(N)'
	local V_con 	 	: display %-8.3f `r(Var)'
	local SD_con 	 	: display %-8.3f `r(sd)'

	// Voucher stats

	summ `X' if vou == 1
	
	local mean_vou	: display %-8.3f `r(mean)'
	local N_vou 		: display %-8.0f `r(N)'
	local V_vou 		: display %-8.3f `r(Var)'
	local SD_vou	 	: display %-8.3f `r(sd)'
	
    // Free stats
	
	summ `X' if fre == 1
	
	local mean_fre   	: display %-8.3f `r(mean)'
	local N_fre	 	    : display %-8.0f `r(N)'
	local V_fre 	 	: display %-8.3f `r(Var)'
	local SD_fre 	 	: display %-8.3f `r(sd)'

			
	foreach M in mean_edu mean_noedu mean_con mean_vou mean_fre {
		local `M'_str = trim("``M''")
	}
		
	file write table1 "`X'" _tab "`mean_edu_str'" _tab  "`mean_noedu_str'" _tab  ///
	"`mean_con_str'" _tab  "`mean_vou_str'" _tab "`mean_fre_str'" _tab _n

	} 
	file close table1 

***The P-value of By Group Diff
* Edu VS No Edu
est clear
foreach X of varlist age male baseusage aware0 harmvision0 gansu RE2 va18 zmath ///
pedu12 hhwealth bboarding pamigrant blackboard{
quietly reg `X' edu, cluster(schid)
matrix define T = r(table)
estadd scalar pvalue = T[4,1]
eststo
}
esttab using table1_pvalue.csv, replace b(%10.4f) se scalars(F pvalue) 
* Voucher VS Control
est clear
foreach X of varlist age male baseusage aware0 harmvision0 gansu RE2 va18 zmath ///
pedu12 hhwealth bboarding pamigrant blackboard{
quietly reg `X' vou if fre == 0, cluster(schid)
matrix define T = r(table)
estadd scalar pvalue = T[4,1]
eststo
}
esttab using table1_pvalue.csv, append b(%10.3f) se scalars(F pvalue) 
* Free VS Control
est clear
foreach X of varlist age male baseusage aware0 harmvision0 gansu RE2 va18 zmath ///
pedu12 hhwealth bboarding pamigrant blackboard{
quietly reg `X' fre if vou == 0, cluster(schid)
matrix define T = r(table)
estadd scalar pvalue = T[4,1]
eststo
}
esttab using table1_pvalue.csv, append b(%10.3f) se scalars(F pvalue) 
* Free VS Voucher
est clear
foreach X of varlist age male baseusage aware0 harmvision0 gansu RE2 va18 zmath ///
pedu12 hhwealth bboarding pamigrant blackboard{
quietly reg `X' fre if con == 0, cluster(schid)
matrix define T = r(table)
estadd scalar pvalue = T[4,1]
eststo
}
esttab using table1_pvalue.csv, append b(%10.3f) se scalars(F pvalue) 
*************************Table 3**********************
******************************************************
***Direct Observed Data
***wear2 in baseline
foreach T of varlist major edu interaction{
bysort `T': sum baseusage
}
***wear2 in endline
foreach T of varlist major edu interaction{
bysort `T': sum wear2
}
***change in wear
gen deltawear = wear2 - baseusage
label var deltawear "change in glasses wear between baseline and endline"
ta deltawear
foreach T of varlist major edu interaction{
bysort `T': sum deltawear
}
***unadjusted RR 
foreach T of varlist major edu interaction{
glm wear2 i.`T', fam(poisson) link(log) nolog eform cluster(schid) 
}
***adjusted RR 
foreach T of varlist major edu interaction{
glm wear2 i.`T' baseusage, fam(poisson) link(log) nolog eform cluster(schid) 
}
***Self-reported Data
***use2 in endline
foreach T of varlist major edu interaction{
bysort `T': sum use2
}
***change in wear
gen deltause = use2 - baseusage
label var deltause "change in glasses use between baseline and endline"
ta deltawear
foreach T of varlist major edu interaction{
bysort `T': sum deltause
}
***unadjusted RR 
foreach T of varlist major edu interaction{
glm use2 i.`T', fam(poisson) link(log) nolog eform cluster(schid) 
}
***adjusted RR 
foreach T of varlist major edu interaction{
glm use2 i.`T' baseusage, fam(poisson) link(log) nolog eform cluster(schid) 
}

**************************Table 4**********************
******************************************************
/*XCM: here we put endline score as dependent variable, and control for baseline score, instead of doing the delta score*/
***Simple Model
est clear
reg ezmath zmath, cluster(schid) 
eststo
xi: reg ezmath i.major zmath, cluster(schid) 
eststo
reg ezmath age zmath, cluster(schid) 
eststo
reg ezmath male zmath, cluster(schid) 
eststo
reg ezmath baseusage zmath, cluster(schid) 
eststo
reg ezmath gansu zmath, cluster(schid)
eststo 
char RE2[omit] 3
xi: reg ezmath i.RE2 zmath, cluster(schid) 
eststo
reg ezmath pedu12 zmath, cluster(schid) 
eststo
xi: reg ezmath i.hhwealth zmath, cluster(schid) 
eststo
reg ezmath bboarding zmath, cluster(schid) 
eststo
reg ezmath pamigrant zmath, cluster(schid)
eststo
char seatchange[omit] 2
xi: reg ezmath i.seatchange zmath, cluster(schid)
eststo
char blackboard[omit] 1
xi: reg ezmath i.blackboard zmath, cluster(schid)
eststo
esttab using table4.csv, replace  b(%10.2f) p(4) wide nocon nostar
*95% ci
esttab using table4.rtf, replace  b(%10.2f) ci wide nocon nostar
***Multiple Model with Covariates of P-value < 0.20 Included 
*coefficiants and p-value
est clear
xi: reg ezmath zmath i.major age gansu pedu12 i.hhwealth bboarding, cluster(schid)
eststo
esttab using table4.csv, replace  b(%10.2f) p(2) wide nocon nostar
*95% ci
esttab using table4.rtf, replace b(%10.2f) ci wide nocon nostar

**************Table 4.B (Subsample Analysis)*********************
*****************************************************************
est clear
forval m = 1/5{
xi: reg ezmath zmath i.major age gansu pedu12 i.hhwealth bboarding if emtea_14 == `m', cluster(schid)
eststo
}
esttab using table4.csv, replace  b(%10.2f) p(2) order(_Imajor_*) nocon

**************************Table 5**********************
******************************************************
***Direct Observed Data
***Simple Model
est clear
xi: glm wear2 i.interaction baseusage, fam(poisson) link(log) nolog eform cluster(schid) 
eststo
xi: glm wear2 age baseusage, fam(poisson) link(log) nolog eform cluster(schid) 
eststo
xi: glm wear2 male baseusage, fam(poisson) link(log) nolog eform cluster(schid) 
eststo
xi: glm wear2 gansu baseusage, fam(poisson) link(log) nolog eform cluster(schid) 
eststo
xi: glm wear2 aware0 baseusage, fam(poisson) link(log) nolog eform cluster(schid)
eststo 
xi: glm wear2 harmvision0 baseusage, fam(poisson) link(log) nolog eform cluster(schid)
eststo
char RE2[omit] 3
xi: glm wear2 i.RE2 baseusage, fam(poisson) link(log) nolog eform cluster(schid) 
eststo
xi: glm wear2 va18 baseusage, fam(poisson) link(log) nolog eform cluster(schid) 
eststo
xi: glm wear2 zmath baseusage, fam(poisson) link(log) nolog eform cluster(schid)
eststo 
xi: glm wear2 pedu12 baseusage, fam(poisson) link(log) nolog eform cluster(schid) 
eststo
xi: glm wear2 i.hhwealth baseusage, fam(poisson) link(log) nolog eform cluster(schid) 
eststo
xi: glm wear2 bboarding baseusage, fam(poisson) link(log) nolog eform cluster(schid) 
eststo
xi: glm wear2 pamigrant baseusage, fam(poisson) link(log) nolog eform cluster(schid) 
eststo
xi: glm wear2 i.blackboard baseusage, fam(poisson) link(log) nolog eform cluster(schid)
eststo

***Full Model adjusted by the basleine wear and covariates with p < 0.20 in the simple model
*age aware0 RE2 va18
est clear
char RE2[omit] 3
xi: glm wear2 i.major baseusage age aware0 i.RE2 va18, fam(poisson) link(log) nolog eform cluster(schid)
xi: glm wear2 edu baseusage age aware0 i.RE2 va18, fam(poisson) link(log) nolog eform cluster(schid) 
xi: glm wear2 i.interaction baseusage age aware0 i.RE2 va18, fam(poisson) link(log) nolog eform cluster(schid) 

***Self-reported Data
***Simple Model
est clear
xi: glm use2 i.interaction baseusage, fam(poisson) link(log) nolog eform cluster(schid) 
eststo
xi: glm use2 age baseusage, fam(poisson) link(log) nolog eform cluster(schid) 
eststo
xi: glm use2 male baseusage, fam(poisson) link(log) nolog eform cluster(schid) 
eststo
xi: glm use2 gansu baseusage, fam(poisson) link(log) nolog eform cluster(schid) 
eststo
xi: glm use2 aware0 baseusage, fam(poisson) link(log) nolog eform cluster(schid)
eststo 
xi: glm use2 harmvision0 baseusage, fam(poisson) link(log) nolog eform cluster(schid)
eststo
char RE2[omit] 3
xi: glm use2 i.RE2 baseusage, fam(poisson) link(log) nolog eform cluster(schid) 
eststo
xi: glm use2 va18 baseusage, fam(poisson) link(log) nolog eform cluster(schid) 
eststo
xi: glm use2 zmath baseusage, fam(poisson) link(log) nolog eform cluster(schid)
eststo 
xi: glm use2 pedu12 baseusage, fam(poisson) link(log) nolog eform cluster(schid) 
eststo
xi: glm use2 i.hhwealth baseusage, fam(poisson) link(log) nolog eform cluster(schid) 
eststo
xi: glm use2 bboarding baseusage, fam(poisson) link(log) nolog eform cluster(schid) 
eststo
xi: glm use2 pamigrant baseusage, fam(poisson) link(log) nolog eform cluster(schid) 
eststo
xi: glm use2 i.blackboard baseusage, fam(poisson) link(log) nolog eform cluster(schid)
eststo

***Full Model adjusted by the basleine wear and covariates with p < 0.20 in the simple model
*aware0 RE2 va18 zmath
est clear
char RE2[omit] 3
xi: glm use2 i.major baseusage aware0 i.RE2 va18 zmath, fam(poisson) link(log) nolog eform cluster(schid)
xi: glm use2 edu baseusage aware0 i.RE2 va18 zmath, fam(poisson) link(log) nolog eform cluster(schid) 
xi: glm use2 i.interaction baseusage aware0 i.RE2 va18 zmath, fam(poisson) link(log) nolog eform cluster(schid) 


***Figure 1
use "Eyeglasses_China.dta",replace
***total pupulation
ta newcome 
***total failed 2nd screening
ta reex
***total eligible to receive glasses
ta givegl
***excluded becuase of not meeting inclusing criteria
ta givegl reex
***allocation
ta interaction if givegl == 1
***follow up
bysort interaction: tab attrition if givegl == 1

***Figure 2
histogram SE2 if givegl == 1, frequ normal
